import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/message_remind_entity.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'message_remind_controller.dart';

class MessageRemindPage extends StatelessWidget {
  final controller = Get.put(MessageRemindController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "消息提醒"),
      body: GetBuilder<MessageRemindController>(
        builder: (controller) {
          return MultiStateWidget(
            child: SmartRefresher(
              controller: controller.refreshController,
              enablePullDown: true,
              enablePullUp: true,
              onRefresh: controller.onRefresh,
              onLoading: controller.onLoading,
              child: Column(
                children: [
                  buildHeaderWidget(),
                  buildListViewWidget(),
                ],
              ),
            ),
            emptyType: MultiEmptyType.message,
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
    );
  }

  Widget buildHeaderWidget() {
    return Container(
      height: 40.w,
      padding: EdgeInsets.only(left: 16.w, right: 8.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            "消息(${controller.orderCount})",
            style: TextStyle(
              color: Colours.color_text_theme,
              fontSize: 14.sp,
            ),
          ),
          IconButton(
            onPressed: () {
              showDeleteDialog();
            },
            icon: Image.asset("images/ic_delete.png"),
          ),
        ],
      ),
    );
  }

  Widget buildListViewWidget() {
    return Expanded(
      child: ListView.builder(
        itemCount: controller.messageRemindList.length,
        shrinkWrap: true,
        physics: BouncingScrollPhysics(),
        controller: controller.scrollController,
        itemBuilder: (context, index) {
          return buildItemWidget(index);
        },
      ),
    );
  }

  Widget buildItemWidget(int index) {
    MessageRemindItem entity = controller.messageRemindList[index];
    String messageType = entity.msgType ?? "";
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {
              controller.goToPage(messageType, entity.id ?? "", entity.msgInfo ?? "", entity.readStatus ?? 0);
            },
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13.w, horizontal: 16.w),
              child: Row(
                children: [
                  buildIconWidget(messageType),
                  SizedBox(
                    width: 16.w,
                  ),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Expanded(
                              child: Text(
                                entity.msgTypeName ?? "",
                                style: TextStyle(
                                  color: Colours.color_text_theme,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.bold,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            SizedBox(
                              width: 10.w,
                            ),
                            Text(
                              entity.timeFormat ?? "",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 12.sp,
                              ),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 3.w,
                        ),
                        Row(
                          children: [
                            Expanded(
                              child: Text(
                                entity.msgContent ?? "",
                                style: TextStyle(
                                  color: Colours.color_6f7082,
                                  fontSize: 12.sp,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            SizedBox(
                              width: 10.w,
                            ),
                            Visibility(
                              visible: entity.readStatus == 0,
                              child: Text(
                                "未读",
                                style: TextStyle(
                                  color: Colours.color_ff3b30,
                                  fontSize: 12.sp,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget buildIconWidget(String messageType) {
    String iconName = "ic_message_transfer.png";
    switch (messageType) {
      case Constant.MESSAGE_REMIND_TYPE_TRANSFER: //公司转账(收款)
        iconName = "ic_message_transfer.png";
        break;
      case Constant.MESSAGE_REMIND_TYPE_AMOUNT:
        iconName = "ic_message_amount.png";
        break;
      default:
    }
    return Image.asset("images/$iconName");
  }

  void showDeleteDialog() {
    showCupertinoDialog(
      barrierDismissible: true,
      context: Get.context!,
      builder: (c) {
        return CupertinoAlertDialog(
          content: Text(
            "确定清除所有消息?",
            style: TextStyle(
              color: Colours.color_text_theme,
              fontSize: 14.sp,
            ),
          ),
          actions: [
            CupertinoDialogAction(
              child: Text(
                "取消",
                style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
              ),
              onPressed: () {
                //Navigator.pop(c);
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: Text(
                "确定",
                style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
              ),
              onPressed: () {
                //Navigator.pop(c);
                Get.back();
                controller.deleteMessage();
              },
            ),
          ],
        );
      },
    );
  }
}
